import 'package:bitcoin_mine/common/global.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../controllers/privacy_controller.dart';
import '../l10n/app_localizations.dart';

class PrivacyAgreementDialog extends StatelessWidget {
  const PrivacyAgreementDialog({super.key});

  @override
  Widget build(BuildContext context) {
    final PrivacyController controller = Get.find<PrivacyController>();
    final l10n = AppLocalizations.of(context);

    return Obx(() {
      // 如果用户已同意隐私协议，不显示弹窗
      if (controller.hasAgreedToPrivacy.value) {
        Global.initApp();
        return const SizedBox.shrink();
      }

      // 如果用户未同意隐私协议，始终显示弹窗
      return Stack(
        children: [
          // 背景遮罩
          Container(
            color: Colors.black.withOpacity(0.8),
            child: Center(
              child: Container(
                width: 320.w,
                margin: EdgeInsets.symmetric(horizontal: 20.w),
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [const Color(0xFF0A0E27), const Color(0xFF1A1F3A)],
                  ),
                  borderRadius: BorderRadius.circular(20.r),
                  border: Border.all(color: const Color(0xFF00FFFF), width: 2),
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xFF00FFFF).withOpacity(0.3),
                      blurRadius: 20,
                      spreadRadius: 5,
                    ),
                  ],
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    // 标题
                    Container(
                      padding: EdgeInsets.all(20.w),
                      decoration: BoxDecoration(
                        color: const Color(0xFF00FFFF).withOpacity(0.1),
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(18.r),
                          topRight: Radius.circular(18.r),
                        ),
                      ),
                      child: Row(
                        children: [
                          Icon(
                            Icons.privacy_tip_outlined,
                            color: const Color(0xFF00FFFF),
                            size: 24.sp,
                          ),
                          SizedBox(width: 12.w),
                          Expanded(
                            child: Text(
                              l10n.privacyAgreement,
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 18.sp,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),

                    // 内容
                    Padding(
                      padding: EdgeInsets.all(20.w),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          // 协议内容
                          Text(
                            l10n.privacyAgreementContent,
                            style: TextStyle(
                              color: Colors.white70,
                              fontSize: 14.sp,
                              height: 1.5,
                            ),
                          ),

                          SizedBox(height: 20.h),

                          // 链接按钮
                          Row(
                            children: [
                              Expanded(
                                child: _buildLinkButton(
                                  l10n.privacyPolicyText,
                                  controller.openPrivacyPolicy,
                                ),
                              ),
                              SizedBox(width: 12.w),
                              Expanded(
                                child: _buildLinkButton(
                                  l10n.termsOfServiceText,
                                  controller.openTermsOfService,
                                ),
                              ),
                            ],
                          ),

                          SizedBox(height: 24.h),

                          // 操作按钮
                          Row(
                            children: [
                              Expanded(
                                child: _buildActionButton(
                                  l10n.disagree,
                                  () => controller.disagreeToPrivacy(),
                                  isPrimary: false,
                                ),
                              ),
                              SizedBox(width: 12.w),
                              Expanded(
                                child: _buildActionButton(
                                  l10n.agreeAndContinue,
                                  () => controller.agreeToPrivacy(),
                                  isPrimary: true,
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      );
    });
  }

  Widget _buildLinkButton(String text, VoidCallback onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 12.w),
        decoration: BoxDecoration(
          color: const Color(0xFF00FFFF).withOpacity(0.1),
          borderRadius: BorderRadius.circular(8.r),
          border: Border.all(
            color: const Color(0xFF00FFFF).withOpacity(0.3),
            width: 1,
          ),
        ),
        child: Text(
          text,
          style: TextStyle(
            color: const Color(0xFF00FFFF),
            fontSize: 12.sp,
            fontWeight: FontWeight.w600,
          ),
          textAlign: TextAlign.center,
        ),
      ),
    );
  }

  Widget _buildActionButton(
    String text,
    VoidCallback onTap, {
    required bool isPrimary,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 12.h),
        decoration: BoxDecoration(
          gradient:
              isPrimary
                  ? LinearGradient(
                    colors: [const Color(0xFF00FFFF), const Color(0xFF0099CC)],
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                  )
                  : null,
          color: isPrimary ? null : const Color(0xFF1A1F3A),
          borderRadius: BorderRadius.circular(8.r),
          border: Border.all(
            color:
                isPrimary
                    ? const Color(0xFF00FFFF)
                    : const Color(0xFF00FFFF).withOpacity(0.3),
            width: 1,
          ),
          boxShadow:
              isPrimary
                  ? [
                    BoxShadow(
                      color: const Color(0xFF00FFFF).withOpacity(0.3),
                      blurRadius: 8,
                      spreadRadius: 1,
                      offset: const Offset(0, 2),
                    ),
                  ]
                  : null,
        ),
        child: Text(
          text,
          style: TextStyle(
            color: isPrimary ? Colors.black : Colors.white,
            fontSize: 14.sp,
            fontWeight: FontWeight.bold,
          ),
          textAlign: TextAlign.center,
        ),
      ),
    );
  }
}
